package  
{
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.media.SoundTransform;
	import org.flixel.FlxSound;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxG;
	
	import flash.display.LoaderInfo;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.system.Security;	
	import flash.media.SoundMixer;
	/**
	 * ...
	 * @author morgan
	 */
	public class TitleState extends FlxState
	{
		[Embed (source = "../data/title.png")] private var img_title:Class;
		[Embed (source = "../data/title_cloud1.png")] private var img_cloud1:Class;
		[Embed (source = "../data/title_cloud2.png")] private var img_cloud2:Class;
		[Embed (source = "../data/title_options.png")] private var img_options:Class;
		[Embed (source = "../data/title_dog.png")] private var img_dog:Class;
		[Embed (source = "../data/title_boss.png")] private var img_boss:Class;
		[Embed (source = "../data/press_space.png")] private var img_white:Class;
		[Embed (source = "../data/ring.png")] private var img_particle:Class;
		[Embed (source = "../data/stage_select.png")] private var img_select:Class;
		[Embed (source = "../data/ss_selector.png")] private var img_selector:Class;
		[Embed (source = "../data/title_arrows.png")] private var img_arrows:Class;
		[Embed (source = "../data/title_title.png")] private var img_hungerdog:Class;
		[Embed (source = "../data/mute.png")] private var img_mute:Class;
		
		public var dog:FlxSprite;
		public var cloudA:FlxSprite;
		public var cloudB:FlxSprite;
		public var boss:FlxSprite;
		public var option:FlxSprite;
		public var particle:FlxSprite;
		public var pressSpace:FlxSprite;
		public var selection:FlxSprite;
		public var selector:FlxSprite;
		public var scrolling:Boolean = false;
		public var backing:Boolean = false;
		public var t:Number = 0;
		public var arrows:FlxSprite;
		public var hd:FlxSprite;
		
		public var time:Number = 0;
		public function TitleState() 
		{

		}
		
		override public function create():void 
		{
			GameState.music = false;
			doKong();
			Saving.loadData();
			
			var bg:FlxSprite = new FlxSprite(0, 0, img_title); add(bg);
			bg.scrollFactor.x = 1.0;
			cloudA = new FlxSprite(30, 70, img_cloud2); add(cloudA); cloudA.velocity.x = 1;
			cloudB = new FlxSprite(130, 90, img_cloud1); add(cloudB);
			cloudA.scrollFactor.x = 1.2;
			cloudB.scrollFactor.x = 1.2;			
			particle = new FlxSprite(220, 210); add(particle);
			particle.loadGraphic(img_particle, true, false, 84, 21);
			particle.addAnimation("default", [0, 1, 2, 3, 4, 4, 4], 14);
			particle.velocity.y = 70;
			particle.scrollFactor.x = 2.0;
			particle.play("default");
			
			pressSpace = new FlxSprite(0, 140, img_white); add(pressSpace);
			pressSpace.scrollFactor.x = 2.0;
			
			option = new FlxSprite(0, 0); add(option);
			option.loadGraphic(img_options, true, false, 320, 240);
			option.frame = 0;
			option.scrollFactor.x = 2.0;
			
			boss = new FlxSprite(40, 50, img_boss); add(boss);
			boss.scrollFactor.x = 1.3;			
			
			dog = new FlxSprite(200, 74, img_dog); add(dog);
			dog.scrollFactor.x = 2.0;
			
			selection = new FlxSprite(462, 0, img_select); add(selection);
			selection.scrollFactor.x = 2.0;
			
			selector = new FlxSprite(460, 18, img_selector); add(selector);
			selector.scrollFactor.x = 2.0;
			
			arrows = new FlxSprite(3, 165, img_arrows); add(arrows);
			arrows.scrollFactor.x = 2.0;
			
			hd = new FlxSprite(0, 0, img_hungerdog); add(hd); hd.scrollFactor.x = 1.5;
			var w:int = letterPositions[Saving.beatenLevelIndex];
			hd.pixels.colorTransform(new Rectangle(w, 0, 320, 100),
				new ColorTransform(1, 1, 1, 1, 0, -255, -189, 0));
			hd.dirty = true;			
			
			var mute:FlxSprite = new FlxSprite(211, 45, img_mute);
			add(mute)
			mute.alpha = 0.5;
			mute.scrollFactor.x = 2.0;
			
			super.create();
			FlxG.flash(0xffffff, 1.0);
		}
		
		override public function update():void 
		{
			if (FlxG.keys.justPressed("M"))
			{
				if (SoundMixer.soundTransform.volume > 0)
				{
					SoundMixer.soundTransform = new SoundTransform(0, 0);
				}
				else
				{
					SoundMixer.soundTransform = new SoundTransform(1, 0);
				}
			}
			time += FlxG.elapsed;
			
			dog.y = Math.sin(time * 3) * 3 + 80;
			
			arrows.alpha = Math.sin(time * 10) * 0.1 + 0.75;
			
			if (cloudA.x < 10) { cloudA.velocity.x = 1.5; }
			else if (cloudA.x > 30) { cloudA.velocity.x = -1.5; }
			boss.y = Math.sin(time / 2) * 1.5 + 50;
			
			cloudB.x = Math.sin(time / 5 - 1) * 4 + 130;	
			
			if (particle.y > dog.y + 160) { particle.y = dog.y + 130; particle.play("default", true); }
			
			if (!scrolling)
			{
				if (FlxG.keys.justPressed("LEFT"))
				{
					if (option.frame == 0) { option.frame = 1; }				
					else { option.frame--; }

					SoundAssets.swap.Play();
					
				}
				if (FlxG.keys.justPressed("RIGHT"))
				{
					option.frame = (option.frame + 1) % 2;
					SoundAssets.swap.Play();
				}
			}
			
			if (scrolling || backing)
			{
				if (backing)
				{
					t = Math.max(0, t - FlxG.elapsed * 0.7);
					if (t <= 0) { scrolling = false; backing = false; }
				}
				else
				{
					t = Math.min(1, t + FlxG.elapsed * 0.7);
				}
				FlxG.camera.scroll.x = Math.cos(t * Math.PI) * -100 + 100;
				
				if (t >= 1)
				{
					if (FlxG.keys.justPressed("UP")) { selector.y -= 65; }
					if (FlxG.keys.justPressed("DOWN")) { selector.y += 65; }
					if (FlxG.keys.justPressed("LEFT")) { selector.x -= 65; }
					if (FlxG.keys.justPressed("RIGHT")) { selector.x += 65; }
					if (selector.x < 460) { selector.x = 590; }
					if (selector.x > 590) { selector.x = 460; }
					if (selector.y < 18) { selector.y = 148; }
					if (selector.y > 148) { selector.y = 18; }
					
					var which:int = (selector.x - 460) / 65 + (selector.y - 18) / 65 * 3 + 1;
					if (which <= Saving.beatenLevelIndex + 1)
					{					
						if (FlxG.keys.justPressed("SPACE"))
						{

								gotoLevel(which);
						}
						selector.alpha = 1.0;
					}
					else
					{
						selector.alpha = 0.4;
					}
					if (FlxG.keys.justPressed("ESCAPE"))
					{
						backing = true;
					}
				}
			}
			
			if (!scrolling && FlxG.keys.justPressed("SPACE")) {
				if (option.frame == 0)
				{
					//FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage1State()); } );
					if (Saving.beatenLevelIndex + 1 > 9)
					{
						gotoLevel(1);
					}
					else
					{
						gotoLevel(Saving.beatenLevelIndex + 1);
					}
				}
				else if (option.frame == 1)
				{
					scrolling = true;
				}
				SoundAssets.eat.Play();
			}
			
			super.update();
		}
		
		public function gotoLevel(which:int):void
		{
			if (which == 1) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new BeforeStage1()); } ); }
			if (which == 2) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage2State()); } ); }
			if (which == 3) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage3State()); } ); }
			if (which == 4) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new BeforeStage4()); } ); }
			if (which == 5) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage5State()); } ); }
			if (which == 6) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage6State()); } ); }
			if (which == 7) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new BeforeStage7()); } ); }
			if (which == 8) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage8State()); } ); }
			if (which == 9) { FlxG.fade(0xff000000, 2, function():void { FlxG.switchState(new Stage9State()); } ); }
		}
		
		
		public var letterPositions:Array = [42, 63, 87, 112, 135, 159, 194, 232, 255, 320];
		override public function draw():void 
		{
			super.draw();
			
			for (var i:int = Saving.beatenLevelIndex + 1; i < 9; i++)
			{
				var x:Number = ((i % 3) * 65 + selection.getScreenXY().x+1);
				var y:Number = Math.floor(i / 3) * 65 + 21;
				//FlxG.camera.buffer.fillRect(
				FlxG.camera.buffer.colorTransform(new Rectangle(x, y, 61, 61), new ColorTransform(0.3, 0.3, 0.3, 1, 0, 0, 0, 0));
			}
			
		}
		
		public function doKong():void
		{
			// The API path. The "shadow" API will load if testing locally. 
			var apiPath:String = "http://www.kongregate.com/flash/API_AS3_Local.swf";

			// Allow the API access to this SWF
			Security.allowDomain(apiPath);

			// Load the API
			var request:URLRequest = new URLRequest(apiPath);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, kongLoaded);
			loader.load(request);
			FlxG.stage.addChild(loader);
		}
		
		public var kongregate:*;
		
		// This function is called when loading is complete
		public function kongLoaded(event:Event):void
		{
			// Save Kongregate API reference
			kongregate = event.target.content;

			// Connect to the back-end
			kongregate.services.connect();

			Saving.kong = kongregate;
		}		
	}

}